package cn.lyq.card.ui.login

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import cn.lyq.card.BuildConfig
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.Const
import cn.lyq.card.global.SPKeyGlobal
import cn.lyq.card.manager.UserManager
import cn.lyq.card.ui.main.MainActivity
import cn.lyq.card.ui.pwd.PwdForgetActivity
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_login.*


class LoginActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(LoginVM::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_login
    }

    private val spaceTime = 700
    private var lastClickTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
    }

    override fun initView() {
        super.initView()
        etPhone.setText(SPUtils.getInstance().getString(SPKeyGlobal.APP.PHONE) ?: "")
        etPwd.setText(SPUtils.getInstance().getString(SPKeyGlobal.APP.PWD) ?: "")
    }

    override fun initListener() {
        super.initListener()
        tvLogin.setClick {
            toLogin()
        }
        tvForgetPwd.setClick {
            startActivity(PwdForgetActivity::class.java)
        }
    }

    private fun toLogin() {
        val phone = etPhone.text.toString()
        val pwd = etPwd.text.toString()
        if (!BuildConfig.DEBUG) {
            if (phone.isEmpty() || phone.length != 11) {
                ToastUtils.showShort(etPhone.hint)
                return
            }
            if (pwd.isEmpty() || pwd.length < Const.PWD_LENGTH_MIN) {
                ToastUtils.showShort(etPwd.hint)
                return
            }
        }
        mModel.login(phone, pwd).observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> {
                    Const.token = it.data?.token ?: ""
                    SPUtils.getInstance().put(SPKeyGlobal.BASE.TOKEN, Const.token)
                    SPUtils.getInstance().put(SPKeyGlobal.APP.LEVEL_NAME, it.data?.level_name ?: "")
                    SPUtils.getInstance().put(SPKeyGlobal.APP.PWD, pwd)
                    UserManager.setPhone(phone)
                    dismissLoading()
                    startActivity(MainActivity::class.java)
                    finish()
                }
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
                Status.LOADING -> showLoading()
            }
        })
    }

    override fun onBackPressed() {
        val now = System.currentTimeMillis()
        if (now - lastClickTime < spaceTime) {
            moveTaskToBack(true)
        } else {
            ToastUtils.showShort(R.string.str_exit_app_tip)
            lastClickTime = now
        }
    }
}